class Login {
    form;
    ids;
    m = new Map();
    elem;
    constructor() {
        this.form = document.querySelector("form");
        this.elem=document.querySelector('.loginss')
        this.form.addEventListener("input", e => this.inputHandler(e)); 
       this.elem.addEventListener('click',e => this.submitHandler(e))
    }
    cleartime(input) {

        return new Promise((resolve, reject) => {
            this.ids = setTimeout(() => {
                clearTimeout(this.ids);
                this.ids = undefined;
                resolve(input.value)
            }, 500)
        })
    }
    async inputHandler(e) {
        if (this.ids) return;
        var value = await this.cleartime(e.target);
        if (this.testinput(e.target.name, value)) {
          if(e.target.name==='user') {this.m.set('input',value)}
          else if(e.target.name==='password'){
            this.m.set('password',value)
          }
            e.target.nextElementSibling.style.display = 'none'
           
        } else {
            e.target.nextElementSibling.style.display = 'block'
        }
    }
    testinput(name, value) {
        switch (name) {
            case 'user':
                return /^1\d{10}$/.test(value);
            case 'password':
                return /^[A-Za-z0-9]{6,16}$/.test(value);
        }

    }
    async submitHandler(e) {
        var arr=this.m.get('input')
       
        var res = await fetch("http://localhost:4020/signIn", {
            method: "post",
            body: JSON.stringify(arr)
        });
        res= await res.json();
    
        if(res.result[0]===undefined) return alert('对不起，您的账号密码有误或没有注册')
       
       
        res=res.result[0].password;
       
        var s=this.m.get('password')

       if( Number(s) ===Number(res) ){
        location.href="./index.html"+"?"+s;
        localStorage.setItem("user",s);
       }else{
           alert('密码错误')
       }
  
    }
}
new Login()